//
//  NextTableViewCell.m
//  PTravel
//
//  Created by dllo on 15/11/15.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "NextTableViewCell.h"

@implementation NextTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changColor) name:@"night" object:nil];
        [self createSubViews];
    }
    return self;
}

- (void)changColor
{
    self.collection.backgroundColor = CELLCOLOR;
}
- (void)createSubViews
{
    [self getStationData];
}


//发现下一站
- (void)getStationData
{
    self.arr = [NSMutableArray array];
    self.arr1 = [NSMutableArray array];
    [AppTools getAFN:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88255639279708&lon=121.5396443687879&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%25209.1&v=1" block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        NSMutableArray *arr1 = [smallDic objectForKey:@"subject"];
        for (NSMutableDictionary *dic in arr1) {
            NextStation *next = [[NextStation alloc] init];
            [next setValuesForKeysWithDictionary:dic];
            [self.arr addObject:next.photo];
            [self.arr1 addObject:next];
        }
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, 260) collectionViewLayout:layout];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        [self.contentView addSubview:self.collection];
        [self.collection registerClass:[NextCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
        self.collection.backgroundColor = CELLCOLOR;
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame = CGRectMake(0, 238, BOUNDS.size.width, 25);
        [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.button setTitle:@"查看更多精彩专题>" forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.collection addSubview:self.button];
    }];
}

#pragma mark === 更多精彩专题
- (void)buttonAction
{
    [self.nextDelegate moreStation];
    
}

//collection方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.image.frame = CGRectMake(5, 0, BOUNDS.size.width-10, 120);
            NSURL *url = [NSURL URLWithString:[self.arr firstObject]];
            [cell.image sd_setImageWithURL:url placeholderImage:IMAGE];
        }else{
            NSURL *url = [NSURL URLWithString:[self.arr objectAtIndex:indexPath.item]];
            [cell.image sd_setImageWithURL:url placeholderImage:IMAGE];
            cell.image.frame = CGRectMake(5, 0, BOUNDS.size.width/2-10, 100);
            cell.image.backgroundColor = [UIColor redColor];
        }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        return CGSizeMake(BOUNDS.size.width, 120);
    }else{
        
        return CGSizeMake(BOUNDS.size.width/2-10, 100);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Recommend *recommend = [self.arr1 objectAtIndex:indexPath.item];
    [self.nextDelegate passStation:recommend];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
